package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.mapper.admin.EmployMapper;
import com.sky.pojo.Employee;
import com.sky.result.PageResult;
import com.sky.service.admin.EmployService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployServiceImpl implements EmployService {
    @Autowired
    public EmployMapper employMapper;

    @Override
    public PageResult findByPage(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        List<Employee> byPage = employMapper.findByPage(employeePageQueryDTO.getName());
        Page<Employee> pageObj = (Page<Employee>) byPage;
        return new PageResult(pageObj.getTotal(),pageObj.getResult());
    }

    @Override
    public void add(EmployeeDTO employeeDTO) {
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime updateTime = LocalDateTime.now();
        Employee employee = new Employee(null, employeeDTO.getUsername(), employeeDTO.getUsername(),
                "123456", employeeDTO.getPhone(), employeeDTO.getSex(), employeeDTO.getIdNumber(),
                1, createTime,updateTime,1L,1L);
        employMapper.add(employee);
    }

    @Override
    public void authority(Integer status, Integer id) {
        employMapper.authority(status,id);
    }

    @Override
    public Employee findById(Integer id) {
        return employMapper.findById(id);
    }

    @Override
    public void update(Employee employee) {
        LocalDateTime updateTime = LocalDateTime.now();
        employee.setUpdateTime(updateTime);
        employMapper.update(employee);
    }
}
